package 
{
	import common.IApp;
	import common.IAppBuilder;
	import common.ILogger;
	import common.ITest;
	import common.ITestBuilder;
	import common.ITestResult;
	import common.Logger;
	import flash.display.Sprite;
	import flash.events.Event;
	import pong.app.builders.PongAppBuilder;
	import pong.builders.PongTestBuilder;
	
	/**
	 * ...
	 * @author Luke Van In
	 */
	public class TestPong extends Sprite 
	{
		
		private var _app:IApp;
		
		
		/**
		 * Constructor
		 */
		public function TestPong():void 
		{
			if (this.stage != null) 
			{
				// movieclip is on stage, initialise the application
				this.initialise();
			}
			else 
			{
				// movieclip is still initialising, wait before loading the application
				this.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
			}
		}
		
		
		/**
		 * Movieclip added to the stage
		 * 
		 * @param	e
		 */
		private function onAddedToStage(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
			
			this.initialise();
		}
		
		
		
		/**
		 * Creates an application instance if one does not already exist
		 * 
		 * @throws	Error	Cannot instance more than one app at a time
		 */
		private function initialise():void 
		{
			var builder:ITestBuilder = new PongTestBuilder(logger);
			
			var test:ITest = builder.buildTest();
			
			var result:ITestResult = test.run();
						
			
			var logger:ILogger = new Logger();
			
			logger.log(result.toString());
			
			
			var statusMessage:String = result.getStatus() ? "passed!" : "failed!";
			
			logger.log(statusMessage)
		}
		
	}
	
}